﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      Global Helper
// Created By:     Parker Zhou (parkerz@wicresoft.com)
// Created Date:   2011-1-30
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;
using CCEntity = ASBCC.Database.EntityFramework.CCEntities;

namespace ASBCC.Business
{
    public static class GlobalHelper
    {
        /// <summary>
        /// put system exception log to SystemExceptionLog table
        /// </summary>
        /// <param name="exception"></param>
        public static void PutExceptionLog(Exception exception,string CIL)
        {
            if (exception == null) return;
            try{
                using (var db = new CCEntity())
                {
                    SystemExceptionLog sysExcetion = new SystemExceptionLog();
                    sysExcetion.ExceptionType = exception.GetType().FullName;
                    sysExcetion.ExceptionMessage = exception.ToString();
                    sysExcetion.ExceptionOccurTime = DateTime.Now;
                    if (!string.IsNullOrEmpty(CIL)) sysExcetion.Trigger = CIL;
                    db.AddToSystemExceptionLogs(sysExcetion);
                    db.SaveChanges();
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// record operation log
        /// </summary>
        /// <param name="employNameEN">NameEN</param>
        /// <param name="module">SystemModule enum</param>
        /// <param name="Message">Message to log</param>
        public static void LogOperation(string employNameEN, SystemModule module, string Message)
        {
            if (string.IsNullOrEmpty(employNameEN) || string.IsNullOrEmpty(Message)) return;
            using (var db = new CCEntities())
            {
                var employee = db.Employees.FirstOrDefault(e => e.NameEN == employNameEN);
                if (employee == null) return;
                OperationLog opl = new OperationLog();
                opl.Employee = employee;
                opl.Module = module.ToString();
                opl.Description = Message;
                opl.Time = DateTime.Now;
                db.AddToOperationLog(opl);
                db.SaveChanges();
            }
        }
    }
}
